<template>
    <el-dialog :title="dialogTitle" :visible.sync="visible" width="520px" @close="handleClose" :close-on-click-modal="false">
        <el-form :model="form" :rules="rules" ref="form" label-width="100px">
            <el-form-item label="字典名称" prop="dictName">
                <el-input v-model="form.dictName" placeholder="请输入字典名称" />
            </el-form-item>
            <el-form-item label="字典类型" prop="dictType">
                <el-input v-model="form.dictType" placeholder="请输入字典类型" />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input type="textarea" :rows="3" v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="handleSubmit">确 定</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { dictTypeAdd, dictTypeEdit } from './api/dictionaryType'

export default {
    name: 'TypeAddOrEdit',
    data() {
        return {
            visible: false,
            submitLoading: false,
            isEdit: false,
            form: {
                dictId: undefined,
                dictName: '',
                dictType: '',
                remark: ''
            },
            rules: {
                dictName: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
                dictType: [{ required: true, message: '请输入字典类型', trigger: 'blur' }]
            }
        }
    },
    computed: {
        dialogTitle() {
            return this.isEdit ? '编辑字典类型' : '新增字典类型'
        }
    },
    methods: {
        openAdd() {
            this.isEdit = false
            this.resetForm()
            this.visible = true
        },
        openEdit(row) {
            this.isEdit = true
            this.form = { dictId: row.dictId, dictName: row.dictName, dictType: row.dictType, remark: row.remark }
            this.visible = true
        },
        resetForm() {
            this.form = { dictId: undefined, dictName: '', dictType: '', remark: '' }
            this.$nextTick(() => this.$refs.form && this.$refs.form.clearValidate())
        },
        handleSubmit() {
            this.$refs.form.validate(async valid => {
                if (!valid) return
                this.submitLoading = true
                try {
                    const api = this.isEdit ? dictTypeEdit : dictTypeAdd
                    await api(this.form)
                    this.$message.success(this.isEdit ? '编辑成功' : '新增成功')
                    this.handleClose()
                    this.$emit('refresh')
                } finally {
                    this.submitLoading = false
                }
            })
        },
        handleClose() {
            this.visible = false
            this.resetForm()
        }
    }
}
</script>

<style lang="scss" scoped>
.dialog-footer { text-align: right; }
</style>


